package com.kitchen.service.impl;

import com.kitchen.common.vo.ResultVO;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.beans.entity.Activity;
import com.kitchen.beans.entity.ActivityGoods;
import com.kitchen.beans.vo.ActivityGoodsVo;
import com.kitchen.mapper.ActivityGoodsMapper;
import com.kitchen.mapper.ActivityMapper;
import com.kitchen.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 78468
 */
@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
@Autowired
    private ActivityGoodsMapper activityGoodsMapper;
    /**
     * 显示厨房活动列表
     */
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public ResultVO kitchenActivityInformationDisplay() {
        ResultVO resultVO = new ResultVO();
        try {
            List<Activity> allKitchenInformation = activityMapper.selectAll();
            log.debug("获取到的厨房信息为:{}",allKitchenInformation);

            resultVO.setCode(ResStatus.SUCCESS);
            resultVO.setMsg(ResStatus.SUCCEEDED_IN_OBTAINING_KITCHEN_ACTIVITY_INFORMATION);
            resultVO.setData(allKitchenInformation);
        } catch (Exception e) {
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.FAILED_TO_GET_KITCHEN_ACTIVITY_INFORMATION);
        }
        return resultVO;
    }

    /**
     * 显示活动详情页里面的商品信息
     * @param activity
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public ResultVO displayOfActiveGoods(Activity activity) {
        ResultVO resultVO = new ResultVO();
        try {
            Example example = new Example(ActivityGoods.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("acAid",activity.getAId());
            List<ActivityGoods> activityGoods = activityGoodsMapper.selectByExample(example);
            List<ActivityGoodsVo> transform = transform(activityGoods);
            if (Objects.nonNull(transform)) {
                resultVO.setCode(ResStatus.SUCCESS);
                resultVO.setMsg(ResStatus.SUCCEEDED_IN_OBTAINING_EVENT_PRODUCT_INFORMATION);
                resultVO.setData(transform);
                return resultVO;
            }
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.FAILED_TO_GET_EVENT_PRODUCT_INFORMATION);
        } catch (Exception e) {
            resultVO.setCode(ResStatus.FAIL);
            resultVO.setMsg(ResStatus.FAILED_TO_GET_EVENT_PRODUCT_INFORMATION);
        }
        return resultVO;
    }

    /**
     * 实体类转换为vo类
     * @param list
     * @return
     */
    public  List<ActivityGoodsVo> transform(List<ActivityGoods> list){
        ArrayList<ActivityGoodsVo> activityGoodsVos = new ArrayList<>();
        for (ActivityGoods activityGoods : list) {
            String[] split = activityGoods.getAcSecondLevelFeatures().split(",");
            ActivityGoodsVo activityGoodsVo = new ActivityGoodsVo();
            BeanUtils.copyProperties(activityGoods,activityGoodsVo);
            activityGoodsVo.setAcSecondLevelFeatures(split);
            activityGoodsVos.add(activityGoodsVo);
        }
        return activityGoodsVos;
    }
}
